#
# Ruby Soul - Ruby Embedded Parsing/Lexing using DSL techniques.
#
# Copyright (c) 2008 - Giancarlo Bellido. All Rights Reserved.
#
#

require 'soul/result'
require 'soul/iterator'

module Soul
  
	class Parser
		
		def initialize(match)
			@match = match
		end

		def include?(input, index)
			@match.include?(input, index)
		end

		def parse(input, index=0)
			Soul::Result.new(@match.include?(input, index))
		end

		def >>(rule)
			Parser.new(Soul::SequenceMatch.new(self, rule))		
		end

	end

  class ParserOld
    
    attr_accessor :input
    
    def initialize(rule, input, skipper)
      @rule  = rule
      @input = input
      @skipper = skipper
      
      @start = 0
      @index = 0
      @length= input.length
    end
    
    def get
      return nil if @index == @length
      
      result = @input[@index]
      @index += 1
      
      return result
    end
    
    #
    # Parses input
    #
    def parse
      @success = false
      
      while (c = self.get) do
        print c
        return self unless @rule.include? (c)
      end
      
      @success = true
      
      return self
    end
    
    def succeed?
      return @success
    end
    
    def to_s
      return "Parsing Results: \n  Success? : " + self.succeed?.to_s + "\n  Stop at  : " + @index.to_s + "\n  Length   : " + @length.to_s
    end
    
  end
  
  def Soul.parse(input, skipper=Soul::nothing, &block)
    parser = Soul::Parser.new(yield, input, skipper)
    parser.parse
    return parser
  end
  
  
  
end
